#encoding:utf-8
'''
Created on 2012/2/15

@author: Netdb_Heng
'''
import sys

if len(sys.argv) <= 2:
    print('Usage: python {0} LongInputFile EliminateOutputFile '.format(sys.argv[0]))
    raise SystemExit

print('Evaluating')
inf_ans = open(sys.argv[1])         #正確解答
inf_pred = open(sys.argv[2])        #預測解答

outf = open(sys.argv[3],'w')        #準確顯示
#outf_Score = open(sys.argv[4],'w')  #分數顯示

lines_ans = inf_ans.readlines()
lines_pred = inf_pred.readlines()

predict_right_arr = []
Accuracy = 0
test_right = 0
test_condiction = 0

avg_Fscore = 0
instance = 0                    #筆數
outf.write("Prediction" + "\t|\t" + "Answer  \n")
for i in range(len(lines_ans)):
    predict_right = 0           #預測正確的
    predict_right_arr.append(0)   
    instance_Accuracy=0 
    
    ans = lines_ans[i].split()
    pred = lines_pred[i].split()
    line_ans = lines_ans[i].split(',')
    line_pred = lines_pred[i].split(',')
    outf.write(pred[0] + "\t|\t" + ans[0] + "\t\t")
    for j in range(len(line_pred)):
        for k in range(len(line_ans)):
            if line_pred[j].split()[0] == line_ans[k].split()[0]:
                predict_right = 1
                predict_right_arr[i]+=1
                break
    if predict_right == 1:
        test_right+=1    
    outf.write(str(predict_right_arr[i]) + "/" + str(len(line_ans)) + '\n\n')
    instance_Accuracy = float(predict_right_arr[i])/len(line_ans)
    if(instance_Accuracy>=float(sys.argv[4])):
        test_condiction+=1
    
Accuracy = float(test_right)/len(lines_ans)
outf.write('\n' + "Accuracy: " + str(Accuracy))
outf.write("Accuracy >= " + sys.argv[4] + " 的有 " + str(test_condiction) + "筆")
        
inf_ans.close();
inf_pred.close();
outf.close();